<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="车主" prop="userId">
        <el-select v-model="dataForm.userId" placeholder="机动车车主">
          <el-option v-for="user in userList" :key="user.userId" :label="user.username" :value="user.userId" />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input v-model="dataForm.plateNumber" placeholder="车牌号"></el-input>
      </el-form-item>
      <el-form-item label="车架号" prop="vin">
        <el-input v-model="dataForm.vin" placeholder="车架号"></el-input>
      </el-form-item>
      <el-form-item label="颜色" prop="vehicleColor">
        <el-select v-model="dataForm.vehicleColor" placeholder="机动车颜色" filterable>
          <el-option v-for="dict in vehicleColorList" :key="dict.id" :label="dict.value" :value="dict.code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="vehicleBrand">
        <el-select v-model="dataForm.vehicleBrand" placeholder="机动车品牌" filterable>
          <el-option v-for="dict in vehicleBrandList" :key="dict.id" :label="dict.value" :value="dict.code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="vehicleType">
        <el-select v-model="dataForm.vehicleType" placeholder="机动车类型" filterable>
          <el-option v-for="dict in vehicleTypeList" :key="dict.id" :label="dict.value" :value="dict.code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="登记时间" prop="vehicleRegtime">
        <el-date-picker v-model="dataForm.vehicleRegtime" placeholder="机动车登记时间" type="date"
                        value-format="yyyy-MM-dd"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        userId: '',
        plateNumber: '',
        vin: '',
        vehicleColor: '',
        vehicleBrand: '',
        vehicleType: '',
        vehicleRegtime: ''
      },
      dataRule: {
        userId: [
          {required: true, message: '车牌号不能为空', trigger: 'blur'}
        ],
        plateNumber: [
          {required: true, message: '车牌号不能为空', trigger: 'blur'},
          {pattern: /^[\u4e00-\u9fa5][A-Z][A-Z0-9]{5}$/, message: '车牌号格式错误', trigger: 'blur'}
        ],
        vin: [
          {required: true, message: '车架号不能为空', trigger: 'blur'},
          {pattern: /^[A-HJ-NPR-Z0-9]{17}$/, message: '车架号格式错误', trigger: 'blur'}
        ],
        vehicleColor: [
          {required: true, message: '机动车颜色不能为空', trigger: 'blur'}
        ],
        vehicleBrand: [
          {required: true, message: '机动车品牌不能为空', trigger: 'blur'}
        ],
        vehicleType: [
          {required: true, message: '机动车类型不能为空', trigger: 'blur'}
        ],
        vehicleRegtime: [
          {required: true, message: '机动车登记时间不能为空', trigger: 'blur'}
        ]
      },
      userList: [],
      vehicleColorList: [],
      vehicleBrandList: [],
      vehicleTypeList: []
    }
  },
  methods: {
    // 获取角色为机动车车主的用户
    getUserList () {
      this.$http({
        url: this.$http.adornUrl('/mvs/owner/list'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.userList = data.userList
        } else {
          this.userList = []
        }
      })
    },
    // 获取机动车类型
    getDictList (type, field) {
      this.$http({
        url: this.$http.adornUrl(`/sys/dict/list/${type}`),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this[field] = data.dictList
        } else {
          this[field] = []
        }
      })
    },
    init (id) {
      this.getUserList()
      this.getDictList('vehicleType', 'vehicleTypeList')
      this.getDictList('vehicleBrand', 'vehicleBrandList')
      this.getDictList('vehicleColor', 'vehicleColorList')
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/mvs/vehicle/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.vehicle.userId
              this.dataForm.plateNumber = data.vehicle.plateNumber
              this.dataForm.vin = data.vehicle.vin
              this.dataForm.vehicleColor = data.vehicle.vehicleColor
              this.dataForm.vehicleBrand = data.vehicle.vehicleBrand
              this.dataForm.vehicleType = data.vehicle.vehicleType
              this.dataForm.vehicleRegtime = data.vehicle.vehicleRegtime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/mvs/vehicle/${!this.dataForm.id ? 'save' : 'update'}`),
            method: !this.dataForm.id ? 'post' : 'put',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'userId': this.dataForm.userId,
              'plateNumber': this.dataForm.plateNumber,
              'vin': this.dataForm.vin,
              'vehicleColor': this.dataForm.vehicleColor,
              'vehicleBrand': this.dataForm.vehicleBrand,
              'vehicleType': this.dataForm.vehicleType,
              'vehicleRegtime': this.dataForm.vehicleRegtime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
